<?php


namespace app\admin\controller\User;


use app\admin\controller\BaseController;
use app\admin\model\User\UserVipCodeModel;
use app\Utils\Util;
use think\Request;

class UserVipCode extends BaseController
{

    /**
     * 分页查询
     * @param Request $request
     * @return array
     */
    public function getUserVipCodeList(Request $request)
    {
        $param = $request->param();
        $where = [];
        $page = $param['page'];
        $limit = $param['limit'];
        $key_word = !empty($param['key_word']) ? $param['key_word'] : "";
        $status = !empty($param['status']) ? $param['status'] : "";
        if (empty($page)) {
            $page = 1;
        }
        if (empty($limit)) {
            $limit = 10;
        }
        if(!empty($key_word)){
            array_push($where, [['uvc.code', '=', $key_word],['u.mobile', '=', $key_word],'or']);
        }
        if (!empty($status)) {
            if ($status == 1) {
                array_push($where, ['uvc.status', '=', 0]);
            }
            if ($status == 2) {
                array_push($where, ['uvc.status', '=', 1]);
            }
            if ($status == -1) {
                array_push($where, ['uvc.status', '=', -1]);
            }
        }
        $list = UserVipCodeModel::getUserVipCodeListWhere($page, $limit, $where,"uvc.*,u.nickname");
        $count = UserVipCodeModel::getUserVipCodeCountWhere($where);
        return Util::returnPageJson(self::CODE_DATA, $count, $list, "");
    }


    /**
     * 添加
     * @param Request $request
     * @return array
     */
    public function addUserVipCode(Request $request)
    {
        $param = $request->param();
        $number = !empty($param['number']) ? $param['number'] : 0;
        if(empty($number)){
            return Util::returnJson(self::CODE_ERROR, null, "请输入数量");
        }
        $list = Util::codeList($number);
        $data = [];
        foreach ($list as $code){
            $arr = [
                'code' => $code,
                'uid'  => 0,
                'status'  => 0,
                'up_time' => date("Y-m-d H:i:s",time()),
                'add_time' => date("Y-m-d H:i:s",time())
            ];
            array_push($data,$arr);
        }
        $flag = UserVipCodeModel::addUserVipCodeMore($data);
        if ($flag) {
            return Util::returnJson(self::CODE_SUCCESS, null, "添加成功");
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "添加失败");
        }
    }

    /**
     * 更新状态
     * @param Request $request
     * @return array
     */
    public function editUserVipCodeType(Request $request)
    {
        $param = $request->param();
        $id = !empty($param['id']) ? $param['id'] : 0;
        $status = !empty($param['status']) ? $param['status'] : 0;
        $save['status'] = $status;
        $flag = UserVipCodeModel::updateUserVipCodeWhere([['id', '=', $id]], $save);
        if ($flag) {
            return Util::returnJson(self::CODE_SUCCESS, null, "修改成功");
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "修改失败1");
        }
    }

}